<template>
  <a-modal
    v-model:open="visible"
    title=""
    width="520px"
    :closable="false"
    :mask-closable="false"
    :footer="null"
    class="refund-confirm-modal"
  >
    <!-- 自定义头部 -->
    <div class="modal-header">
      <div class="warning-icon">
        <ExclamationCircleOutlined />
      </div>
      <div class="header-content">
        <h3>确认申请退款</h3>
        <p>此操作将申请退款，请仔细核对以下信息</p>
      </div>
    </div>

    <!-- 订单信息 -->
    <div class="order-info-section">
      <h4><FileTextOutlined /> 订单信息</h4>
      <div class="info-grid">
        <div class="info-item">
          <label>订单号</label>
          <span class="value">{{ orderData.payOrderId }}</span>
        </div>
        <div class="info-item">
          <label>商户订单号</label>
          <span class="value">{{ orderData.merchantOrderNo }}</span>
        </div>
        <div class="info-item">
          <label>订单金额</label>
          <span class="value amount">¥{{ formatAmount(orderData.orderAmount) }}</span>
        </div>
        <div class="info-item">
          <label>支付时间</label>
          <span class="value">{{ orderData.orderPayTime || '未知' }}</span>
        </div>
        <div class="info-item">
          <label>支付方式</label>
          <span class="value">
            <a-tag>{{ getPayMethodText(orderData.payMethod) }}</a-tag>
          </span>
        </div>
        <div class="info-item">
          <label>订单状态</label>
          <span class="value">
            <a-tag color="green">{{ getStatusText(orderData.status) }}</a-tag>
          </span>
        </div>
      </div>
    </div>

    <!-- 退款信息 -->
    <div class="refund-info-section">
      <h4><MoneyCollectOutlined /> 退款信息</h4>
      <div class="refund-form">
        <div class="form-item">
          <label>退款类型</label>
          <a-radio-group v-model:value="refundType" @change="handleRefundTypeChange">
            <a-radio value="full">全额退款</a-radio>
            <a-radio value="partial">部分退款</a-radio>
          </a-radio-group>
        </div>
        <div v-if="refundType === 'partial'" class="form-item">
          <label>退款金额</label>
          <a-input-number
            v-model:value="refundAmount"
            :min="0.01"
            :max="maxRefundAmount"
            :precision="2"
            style="width: 200px"
            placeholder="请输入退款金额"
          >
            <template #addonBefore>¥</template>
          </a-input-number>
          <span class="amount-tip">最大可退款：¥{{ formatAmount(orderData.orderAmount) }}</span>
        </div>
        <div class="form-item">
          <label>退款原因</label>
          <a-select v-model:value="refundReason" style="width: 300px" placeholder="请选择退款原因">
            <a-select-option value="user_request">用户申请退款</a-select-option>
            <a-select-option value="order_error">订单异常</a-select-option>
            <a-select-option value="quality_issue">商品质量问题</a-select-option>
            <a-select-option value="delivery_delay">发货延迟</a-select-option>
            <a-select-option value="duplicate_payment">重复支付</a-select-option>
            <a-select-option value="system_error">系统错误</a-select-option>
            <a-select-option value="merchant_refund">商户主动退款</a-select-option>
            <a-select-option value="other">其他原因</a-select-option>
          </a-select>
        </div>
        <div v-if="refundReason === 'other'" class="form-item">
          <label>详细说明</label>
          <a-textarea
            v-model:value="refundReasonDetail"
            :rows="3"
            placeholder="请详细说明退款原因..."
            :maxlength="200"
            show-count
          />
        </div>
      </div>
    </div>

    <!-- 重要提醒 -->
    <div class="warning-section">
      <h4><WarningOutlined /> 重要提醒</h4>
      <ul class="warning-list">
        <li>退款申请提交后无法撤销</li>
        <li>退款处理可能需要1-7个工作日</li>
        <li>退款金额将原路返回到支付账户</li>
        <li>部分退款可能影响订单状态</li>
      </ul>
    </div>

    <!-- 操作按钮 -->
    <div class="modal-footer">
      <a-space>
        <a-button @click="handleCancel">取消</a-button>
        <a-button
          type="primary"
          danger
          :loading="loading"
          :disabled="!isFormValid"
          @click="handleConfirm"
        >
          确认申请退款
        </a-button>
      </a-space>
    </div>
  </a-modal>
</template>

<script lang="ts" setup>
import { ref, reactive, computed } from 'vue';
import {
  ExclamationCircleOutlined,
  FileTextOutlined,
  MoneyCollectOutlined,
  WarningOutlined
} from '@ant-design/icons-vue';

interface OrderData {
  payOrderId?: string;
  merchantOrderNo?: string;
  orderAmount?: number;
  orderPayTime?: string;
  payMethod?: number;
  status?: number;
}

const emit = defineEmits<{
  confirm: [data: any]
}>();

const visible = ref(false);
const loading = ref(false);
const orderData = reactive<OrderData>({});

// 退款表单数据
const refundType = ref<'full' | 'partial'>('full');
const refundAmount = ref<number>(0);
const refundReason = ref<string>('');
const refundReasonDetail = ref<string>('');

// 计算最大退款金额
const maxRefundAmount = computed(() => {
  return orderData.orderAmount ? orderData.orderAmount / 100 : 0;
});

// 表单验证
const isFormValid = computed(() => {
  if (!refundReason.value) return false;
  if (refundType.value === 'partial') {
    if (!refundAmount.value || refundAmount.value <= 0) return false;
    if (refundAmount.value > maxRefundAmount.value) return false;
  }
  if (refundReason.value === 'other' && !refundReasonDetail.value.trim()) return false;
  return true;
});

function show(data: OrderData) {
  Object.assign(orderData, data);
  resetForm();
  visible.value = true;
}

function resetForm() {
  refundType.value = 'full';
  refundAmount.value = maxRefundAmount.value;
  refundReason.value = '';
  refundReasonDetail.value = '';
  loading.value = false;
}

function handleRefundTypeChange() {
  if (refundType.value === 'full') {
    refundAmount.value = maxRefundAmount.value;
  } else {
    refundAmount.value = 0;
  }
}

function handleCancel() {
  visible.value = false;
}

async function handleConfirm() {
  if (!isFormValid.value) return;

  loading.value = true;

  try {
    const refundData = {
      ...orderData,
      refundType: refundType.value,
      refundAmount: refundType.value === 'full' ? maxRefundAmount.value : refundAmount.value,
      refundReason: refundReason.value,
      refundReasonDetail: refundReasonDetail.value
    };

    // 模拟API调用延迟
    await new Promise(resolve => setTimeout(resolve, 1000));

    emit('confirm', refundData);
    visible.value = false;
  } finally {
    loading.value = false;
  }
}

function formatAmount(amount?: number): string {
  if (!amount) return '0.00';
  return (amount / 100).toFixed(2);
}

function getPayMethodText(payMethod?: number): string {
  const methodMap: Record<number, string> = {
    1: '微信APP支付',
    2: '微信H5支付',
    3: '微信小程序支付',
    4: '支付宝APP支付',
    5: '支付宝H5支付',
    6: '支付宝小程序支付'
  };
  return methodMap[payMethod || 0] || '未知';
}

function getStatusText(status?: number): string {
  const statusMap: Record<number, string> = {
    0: '待支付',
    1: '支付中',
    2: '支付成功',
    3: '支付失败',
    4: '已关闭'
  };
  return statusMap[status || 0] || '未知';
}

defineExpose({ show });
</script>

<style scoped>
.refund-confirm-modal :deep(.ant-modal-content) {
  padding: 0;
  overflow: hidden;
}

.modal-header {
  display: flex;
  align-items: flex-start;
  padding: 24px 24px 0;
  margin-bottom: 24px;
}

.warning-icon {
  font-size: 24px;
  color: #faad14;
  margin-right: 16px;
  margin-top: 2px;
}

.header-content h3 {
  margin: 0 0 4px 0;
  font-size: 18px;
  font-weight: 600;
  color: #262626;
}

.header-content p {
  margin: 0;
  color: #666;
  font-size: 14px;
}

.order-info-section,
.refund-info-section,
.warning-section {
  padding: 0 24px;
  margin-bottom: 24px;
}

.order-info-section h4,
.refund-info-section h4,
.warning-section h4 {
  display: flex;
  align-items: center;
  margin: 0 0 16px 0;
  font-size: 16px;
  font-weight: 600;
  color: #262626;
}

.order-info-section h4 .anticon,
.refund-info-section h4 .anticon,
.warning-section h4 .anticon {
  margin-right: 8px;
  color: #1890ff;
}

.info-grid {
  display: grid;
  grid-template-columns: 1fr 1fr;
  gap: 16px;
}

.info-item {
  display: flex;
  flex-direction: column;
}

.info-item label {
  font-size: 12px;
  color: #666;
  margin-bottom: 4px;
}

.info-item .value {
  font-size: 14px;
  color: #262626;
  font-weight: 500;
}

.info-item .value.amount {
  color: #f5222d;
  font-size: 16px;
  font-weight: 600;
}

.refund-form {
  display: flex;
  flex-direction: column;
  gap: 16px;
}

.form-item {
  display: flex;
  flex-direction: column;
}

.form-item label {
  font-size: 14px;
  color: #262626;
  margin-bottom: 8px;
  font-weight: 500;
}

.amount-tip {
  font-size: 12px;
  color: #666;
  margin-top: 4px;
}

.warning-section {
  background: #fff7e6;
  border: 1px solid #ffd591;
  border-radius: 8px;
  margin: 0 24px 24px;
  padding: 16px;
}

.warning-section h4 .anticon {
  color: #faad14;
}

.warning-list {
  margin: 0;
  padding-left: 20px;
}

.warning-list li {
  color: #d46b08;
  font-size: 14px;
  margin-bottom: 4px;
}

.modal-footer {
  padding: 16px 24px 24px;
  display: flex;
  justify-content: flex-end;
  border-top: 1px solid #f0f0f0;
  background: #fafafa;
}
</style>